# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Ryan Algar <ryzer@arch-linux>
# Upstream: TheWaveWarden (Frederik Siepe) <info AT thewavewarden DOT com>

pkgbase=odin2-synthesizer
pkgname=(
  'odin2-synthesizer'
  'odin2-synthesizer-common'
  'odin2-synthesizer-standalone'
  'odin2-synthesizer-vst3'
  'odin2-synthesizer-lv2'
  'odin2-synthesizer-clap'
)
pkgver=2.3.4
pkgrel=2
pkgdesc='24 voice polyphonic synthesizer with modulation and FX'
arch=('loong64' 'x86_64')
url='https://www.thewavewarden.com/odin2'
license=('GPL3')
groups=('pro-audio')
_common_depends=(
  'glibc'
  'gcc-libs'
  'alsa-lib'
  'libglvnd'
  'freetype2'
)
makedepends=(
  "${_common_depends[@]}"
  'git'
  'cmake'
  'curl'
  'lv2'
  'mesa'
  'ttf-font'
  'jack'
  'webkit2gtk'
  'libx11'
  'gendesk'
  'hicolor-icon-theme'
)
_commit='57272428423eb2655b4d482aeb547bf2fb4d2cbe'
source=(
  "$pkgbase::git+https://github.com/TheWaveWarden/odin2#commit=$_commit"
  'add-missing-include.patch'
  'fix-lv2-include.patch'
  'github.com-surge-synthesizer-tuning-library::git+https://github.com/surge-synthesizer/tuning-library'
  'github.com-juce-framework-JUCE::git+https://github.com/juce-framework/JUCE'
  'github.com-free-audio-clap-juce-extensions::git+https://github.com/free-audio/clap-juce-extensions'
  'github.com-lv2-porting-project-JUCE::git+https://github.com/lv2-porting-project/JUCE'
  'github.com-free-audio-clap::git+https://github.com/free-audio/clap'
  'github.com-free-audio-clap-helpers::git+https://github.com/free-audio/clap-helpers'
)
b2sums=('SKIP'
        '6025efd0d3b66d7638ea3f9facbc29f9b06f0101dcff317ecab5b6fdea3d31bb48b29b9858d6e0025ae28b72415a7b6e98ab39d097e466b1e5d760743552c55c'
        '3f90bce1520a648ccc0b46b54a7eaebfb6d2e669ffef545df9e80e8f1724a7ae0f1ae15e21c8e5fa29d667a78c8db40774e917ce1049ebcf90dae19f96ce1f24'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP')

pkgver() {
  cd "$pkgbase"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgbase"

  # setup git submodules
  git submodule init
  git config submodule.tuning-library.url "$srcdir/github.com-surge-synthesizer-tuning-library"
  git config submodule.libs/JUCE.url "$srcdir/github.com-juce-framework-JUCE"
  git config submodule.libs/clap-juce-extensions.url "$srcdir/github.com-free-audio-clap-juce-extensions"
  git config submodule.libs/libs/JUCELV2.url "$srcdir/github.com-lv2-porting-project-JUCE"
  git -c protocol.file.allow=always submodule update

  # setup git submodules for clap-juce-extensions
  pushd libs/clap-juce-extensions
  git submodule init
  git config submodule.clap-libs/clap.url "$srcdir/github.com-free-audio-clap"
  git config submodule.clap-libs/clap-helpers.url "$srcdir/github.com-free-audio-clap-helpers"
  git -c protocol.file.allow=always submodule update
  popd

  # fix FTBFS due to missing include directives
  pushd libs/JUCELV2
  git apply "$srcdir/add-missing-include.patch"
  git apply "$srcdir/fix-lv2-include.patch"
  popd

  # generate desktop file
  gendesk -n \
    --exec odin2 \
    --name Odin2 \
    --pkgname odin2 \
    --pkgdesc "$pkgdesc" \
    --icon odin2 \
    --genericname "Virtual polyphonic synthesizer"
}

build() {
  cmake \
    -S "$pkgbase" \
    -B build \
    -DCMAKE_BUILD_TYPE='Release'

  cmake --build build
}

package_odin2-synthesizer() {
  depends=($pkgbase-{common,standalone,vst3,clap,lv2}=$pkgver)
  install='odin2-synthesizer.install'
}

package_odin2-synthesizer-common() {
  pkgdesc='Common files for Odin2'

  cd "$pkgbase"

  install -vDm644 \
    -t "$pkgdir/usr/share/doc/$pkgbase" \
    README.md screenshot.png manual/manual.pdf
}

package_odin2-synthesizer-standalone() {
  pkgdesc+=' - standalone'
  depends=(
    "odin2-synthesizer-common=$pkgver"
    'hicolor-icon-theme'
    "${_common_depends[@]}"
  )
  optdepends=(
    'jack: audio backend'
    'alsa-lib: audio backend'
  )

  # binary
  pushd build/Odin2_artefacts/Release
  install -vDm755 Standalone/Odin2 "$pkgdir/usr/bin/odin2"
  popd

  cd "$pkgbase"

  # desktop file
  install -vDm644 -t "$pkgdir/usr/share/applications" odin2.desktop

  # application icon
  install -vDm644 manual/graphics/logo.png "$pkgdir/usr/share/icons/hicolor/512x512/apps/odin2.png"
}

package_odin2-synthesizer-vst3() {
  pkgdesc+=' - VST3 plugin'
  groups+=('vst3-plugins')
  depends=(
    "odin2-synthesizer-common=$pkgver"
    "${_common_depends[@]}"
    'vst3-host'
  )

  cd build/Odin2_artefacts/Release

  install -vd "$pkgdir/usr/lib/vst3"

  cp -vr VST3/Odin2.vst3 "$pkgdir/usr/lib/vst3"
}

package_odin2-synthesizer-lv2() {
  pkgdesc+=' - LV2 plugin'
  groups+=('lv2-plugins')
  depends=(
    "odin2-synthesizer-common=$pkgver"
    "${_common_depends[@]}"
    'lv2-host'
  )
  cd build/Odin2_artefacts/Release

  install -vd "$pkgdir/usr/lib/lv2"

  cp -vr LV2/Odin2.lv2 "$pkgdir/usr/lib/lv2"
}

package_odin2-synthesizer-clap() {
  pkgdesc+=' - CLAP plugin'
  groups+=('clap-plugins')
  depends=(
    "odin2-synthesizer-common=$pkgver"
    "${_common_depends[@]}"
    'clap-host'
  )
  cd build/Odin2_artefacts/Release

  install -vDm755 -t "$pkgdir/usr/lib/clap" CLAP/Odin2.clap
}
